<template>
  <div style="margin-right:50px;">
    <el-form :model="form" status-icon :rules="rules" ref="form" label-width="180px" class="demo-ruleForm" 
      style="margin-top: 50px;" size="mini">
      <el-row>
        <el-col :span="8">
          <el-form-item label="产品组合" prop="t8_sys_portfol_id">
              <el-select v-model="form.t8_sys_portfol_id" @change="t8_sys_portfol_id_change_fnc" 
                filterable
                clearable
                placeholder="请选择产品组合">
                <el-option
                  v-for="item in dict.t8_sys_portfol_id"
                  :key="item.value"
                  :label="item.text"
                  :value="item.value">
                </el-option>
              </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="产品/SPV" prop="t8_prod_info_id">
            <el-select v-model="form.t8_prod_info_id" placeholder="请选择产品/SPV" 
              filterable
              clearable
              @change="t8_prod_info_id_change_fnc" >
              <el-option
                v-for="item in dict.t8_prod_info_id"
                :key="item.value"
                :label="item.text"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="产品/SPV余额(元)" prop="prod_principalbln">
            <el-input v-model="form.prod_principalbln" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="分红日期:" prop="tradedate">
            <el-date-picker
              v-model="form.tradedate"
              type="date"
              class="venus-input-middle venus-input-date"
              value-format="yyyyMMdd"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="交易市场" prop="marketplaceid">
            <el-select v-model="form.marketplaceid"
              filterable
              clearable
              placeholder="请选择交易市场">
              <el-option
                v-for="(text,val) in dict.marketplaceid"
                :key="val"
                :label="text"
                :value="val">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="会计分类" prop="account_type">
            <el-select v-model="form.account_type" placeholder="请选择交易市场"
              filterable
              clearable
            >
              <el-option
                v-for="item in dict.account_type"
                :key="item.value"
                :label="item.text"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="基金代码:" prop="ftool_code">
            <el-input v-model="form.ftool_code" @blur="ftool_code_blur_fnc"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="基金名称:" prop="ftool_name">
            <el-input v-model="form.ftool_name" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="核算类型:" prop="acct_mod">
            <el-select v-model="form.acct_mod" placeholder="请选择核算类型" disabled>
              <el-option
                v-for="(text,val) in dict.acct_mod"
                :key="val"
                :label="text"
                :value="val">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="持仓余额(份):" prop="positionbln">
            <el-input v-model="form.positionbln" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="分红方式:" prop="direction">
            <el-select v-model="form.direction" placeholder="请选择分红方式" @change="direction_change_fnc">
              <el-option
                v-for="(text,val) in dict.direction"
                :key="val"
                :label="text"
                :value="val">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="单位净值（元）:" prop="cencleanprice" v-show="public_params.show_cencleanprice">
            <el-input v-model="form.cencleanprice" @blur="cencleanprice_blue_fnc"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="分红金额（元）:" prop="grossamount">
            <el-input v-model="form.grossamount" @blur="grossamount_blue_fnc"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="分红份额（份）:" prop="faceamount" v-show="public_params.show_faceamount">
            <el-input v-model="form.faceamount" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="备注:" prop="note">
            <el-input v-model="form.note"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="资金账号:" prop="account_code" v-show="public_params.show_account_code">
            <el-input v-model="form.account_code"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="资金账户名称:" prop="account_name" v-show="public_params.show_account_name">
            <el-input v-model="form.account_name"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="开户行名称:" prop="opening_account_name" v-show="public_params.show_opening_account_name">
            <el-input v-model="form.opening_account_name"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="开户行行号:" prop="opening_account_num" v-show="public_params.show_opening_account_num">
            <el-input v-model="form.opening_account_num"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<script>
  import * as kk from '@/utils/k-utils'
  export default {
    name:"trade_fund_fh",
    props:{
        parent:Object  //父组件this
    },
    data() {
      return {
        //数据字典
        dict:{
          t8_sys_portfol_id:[],//投资组合
          t8_prod_info_id:[],//理财产品
          marketplaceid:[],
          acct_mod:[],
          account_type:[],
          direction:[]
        },
        //form表单
        form:{
          t8_sys_portfol_id:'',
          t8_prod_info_id:'',
          prod_principalbln:'',
          tradedate:'',
          marketplaceid:'',
          ftool_code:'',
          ftool_name:'',
          acct_mod:'',
          fund_type:'',
          account_type:'',
          positionbln:'',
          direction:'',
          cencleanprice:'',
          faceamount:'',
          grossamount:'',
          note:'',
          account_code:'',
          account_name:'',
          opening_account_name:'',
          opening_account_num:''
        },
        rules:{
          t8_sys_portfol_id:[
            {required: true, message: '请输入产品组合', trigger: 'change'}
          ],
          t8_prod_info_id:[
            {required: true, message: '请输入产品/SPV', trigger: 'change'}
          ],
          tradedate:[
            {required: true, message: '请输入交易日期', trigger: 'change'}
          ],
          marketplaceid:[
            {required: true, message: '请输入交易市场', trigger: 'change'}
          ],
          account_type:[
            {required: true, message: '请输入会计分类', trigger: 'change'}
          ],
          ftool_code:[
            {required: true, message: '请输入基金代码', trigger: 'change'}
          ],
          direction:[
            {required: true, message: '请输入分红方式', trigger: 'change'}
          ],
          cencleanprice:[
            {required: true, message: '请输入单位净值', trigger: 'change'}
          ],
          grossamount:[
            {required: true, message: '请输入分红金额', trigger: 'blur'},
            {validator:this.validator_money,trigger: 'blur'}
          ]
        },
        public_params:{
          show_cencleanprice:true,
          show_faceamount:true,
          show_account_code:true,
          show_account_name:true,
          show_opening_account_name:true,
          show_opening_account_num:true
        }
      };
    },
    mounted:function(){
      //加载投组下拉框
      kk.k_load_exeid('public','selectPortfol').then(rep=>{
        this.dict.t8_sys_portfol_id=rep.rows;
      });
      //加载会计分类下拉框
      kk.k_load_exeid('fund','M82331EQ004').then(rep=>{
        this.dict.account_type=rep.rows;
      });
      kk.k_load_dict(this,"marketplaceid","t8_market_place");
      kk.k_load_dict(this,"acct_mod","t8_busi_account_type");
      kk.k_load_dict(this,"direction","t8_fund_dividends");
    },
    methods: {
      //校验输入金额
      validator_money(rule,value,callback){
        if(value < 0.01){
          return new callback(new Error("输入金额必须大于0"));
        }
        callback();
      },
      //投资组合变动
      t8_sys_portfol_id_change_fnc(){
        kk.k_load_exeid('public','selectProd',{t8_sys_portfol_id:this.form.t8_sys_portfol_id,tradedate:this.form.tradedate}).then(rep=>{
          this.dict.t8_prod_info_id=rep.rows;
        });
      },
      //理财产品变动
      t8_prod_info_id_change_fnc(val){
        if(val != ""){
          kk.k_load_exeid('data','getProdBalance',{t8_prod_info_id:this.form.t8_prod_info_id,queryDate:this.form.tradedate}).then(rep=>{
            this.form.prod_principalbln=rep.rows[0].prod_balance;
          });
        }
      },
      //基金代码失去焦点
      ftool_code_blur_fnc(){
        if(this.form.ftool_code != ""){
          kk.k_load_exeid('fund','M826EQ009B',{fund_code:this.form.ftool_code,market:this.form.marketplaceid,trade_date:this.form.tradedate}).then(rep=>{
            this.form.ftool_name=rep.rows[0].fund_name;
            this.form.fund_type=rep.rows[0].fund_type;
            this.form.acct_mod=rep.rows[0].acct_mod;
          });
          //查询基金持有份额
          kk.k_load_exeid('data','getAllPosition_vm',
            {t8_sys_portfol_id:this.form.t8_sys_portfol_id,t8_sys_adtype_id:this.form.t8_sys_adtype_id,
              bond_market:this.form.marketplaceid,ftool_code:this.form.ftool_code,
              account_type:this.form.account_type,t8_prod_info_id:this.form.t8_prod_info_id,
              workday:this.form.tradedate})
          .then(rep=>{
            this.form.positionbln=rep.rows[0].faceamount;//持仓余额
          });
        }
      },
      //分红方式
      direction_change_fnc(){
        this.form.cencleanprice="";
        this.form.faceamount="";
        this.form.grossamount="";
        if(this.form.direction == "1"){//现金分红
          this.public_params.show_cencleanprice=false;
          this.rules.cencleanprice[0].required=false;//非必填
          this.public_params.show_faceamount=false;
          this.public_params.show_account_code=true;
          this.public_params.show_account_name=true;
          this.public_params.show_opening_account_name=true;
          this.public_params.show_opening_account_num=true;
        }else{
          this.public_params.show_cencleanprice=true;
          this.rules.cencleanprice[0].required=true;//必填
          this.public_params.show_faceamount=true;
          this.public_params.show_account_code=false;
          this.public_params.show_account_name=false;
          this.public_params.show_opening_account_name=false;
          this.public_params.show_opening_account_num=false;
        }
      },
      //单位净值
      cencleanprice_blue_fnc(){
        if(this.form.direction == "-1"){//红利再投
          if(this.form.cencleanprice != "" && this.form.grossamount != ""){
            this.form.faceamount=parseFloat((this.form.cencleanprice*this.form.grossamount),2);
          }
        }else{
          this.form.faceamount="";
        }
      },
      //分红金额
      grossamount_blue_fnc(){
        this.cencleanprice_blue_fnc();
      },
      //获取表单参数
      getFormParams(formName){
        let isValidate=false;
        this.$refs[formName].validate((valid) => {
          isValidate=valid;
        });
        if(isValidate){
          return this.form;
        }else{
          return false;
        }
      },
      //重置表单
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>

<style scoped>
</style>
